﻿using Cxuu.Common;
using Cxuu.Models;
using Cxuu.Service;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace Cxuu.Web.Admin
{
    [Area("Admin")]
    [Authorize]
    public class CategoryController : Base
    {
        private readonly ArticleCateService _articleCateService;
        private readonly AdminUserGroupService _adminUserGroupService;
        private readonly LogService _logService;
        public CategoryController(ArticleCateService articleCateService, AdminUserGroupService adminUserGroupService, LogService logService)
        {
            _articleCateService = articleCateService;
            _adminUserGroupService = adminUserGroupService;
            _logService = logService;
        }


        /// <summary>
        /// 输出JSON 树形栏目列表 内容管理的菜单列表使用
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<IActionResult> GetTree()
        {
            ViewAdminUser adminUser = await UserClaims();
            List<ArticleCate> cates;
            if (adminUser.Gid != 1)
            {
                int[] cateIdChannl = await _articleCateService.GetRoleChannlAsync(adminUser.Gid);
                cates = await _articleCateService.CateRoleListAsync(cateIdChannl);
            }
            else
            {
                cates = await _articleCateService.GetAllAsync();
            }

            List<ViewArticleCate> treeModels = new();
            CategoryExtensions.IndentedCate(cates, treeModels);
            
            return Json(treeModels);
        }


        public async Task<IActionResult> IndexAsync(int json = 0)
        {
            if (json == 1)
            {
                var pagedList = await _articleCateService.GetCategoryListAsync();
                return Json(new { code = 0, data = pagedList });
            }
            return View();
        }


        public async Task<IActionResult> AddOrEditAsync(int? id, int edit = 0, int pid = 0)
        {
            if (edit == 1)
            {
                var data = await _articleCateService.GetInfo(id);
                return Json(new { status = 1, data });
            }
            var articleCateData = await _articleCateService.GetCategoryListAsync();
            ViewBag.articleCateData = articleCateData;
            ViewBag.id = id;
            ViewBag.pid = pid;
            return View();

        }


        [HttpPost]
        [XssFieldFilter]//防Xss
        public async Task<IActionResult> CreatePostAsync(EditArticleCate cateModel)
        {
            if (ModelState.IsValid)
            {
                ArticleCate cate = new()
                {
                    Name = cateModel.Name,
                    Pid = cateModel.Pid,
                    Theme = cateModel.Theme,
                    Type = cateModel.Type,
                    Num = cateModel.Num,
                    Sort = cateModel.Sort
                };
                await _articleCateService.InsertAsync(cate);
                await _logService.InsertLogSqlAsync(await UserInfoAsync(), "新增栏目");
                return Json(new { status = 1, msg = "新增成功！" });
            }
            else
            {
                var error = ModelState.GetErrorMessages();
                return Json(new { status = 0, msg = error });
            }
        }


        [HttpPost, ActionName("EditPost")]
        [XssFieldFilter]//防Xss
        public async Task<IActionResult> EditPostAsync(EditArticleCate cateModel, int id, int dyEdit)
        {
            if (dyEdit == 1)
            {
                ArticleCate articleCate = await _articleCateService.GetInfo(id);
                articleCate.Sort = cateModel.Sort;
                await _articleCateService.UpdateAsync(articleCate);
                return Json(new { status = 1, msg = "修改成功！" });
            }

            if (ModelState.IsValid)
            {
                ArticleCate articleCate = await _articleCateService.GetInfo(id);
                articleCate.Pid = cateModel.Pid;
                articleCate.Name = cateModel.Name;
                articleCate.Theme = cateModel.Theme;
                articleCate.Sort = cateModel.Sort;
                articleCate.Num = cateModel.Num;
                articleCate.Type = cateModel.Type;
                await _articleCateService.UpdateAsync(articleCate);
                await _logService.InsertLogSqlAsync(await UserInfoAsync(), " 修改栏目" + articleCate.Name + " id " + id);
                return Json(new { status = 1, msg = "修改成功！" });
            }
            else
            {
                var error = ModelState.GetErrorMessages();
                return Json(new { status = 0, msg = error });
            }
        }

        [HttpPost, ActionName("Delete")]
        public async Task<IActionResult> DeleteAsync(int id)
        {
            if (await _articleCateService.DeleteByPriKeyAsync(id) > 0)
            {
                await _logService.InsertLogSqlAsync(await UserInfoAsync(), "删除栏目,Id " + id);
                return Json(new { status = 1, msg = "删除成功！" });
            }
            else
                return Json(new { status = 0, msg = "删除失败！可能因为当前栏目下有内容！" });
        }

    }
}
